﻿#include "VPoint.h"
#include <QtMath>

VPoint::VPoint()
{
    this->X = 0.0;
    this->Y = 0.0;
}

VPoint::~VPoint(){}

VPoint::VPoint(double x, double y):X(x),Y(y){}

double VPoint::length() const
{
    return qSqrt(qPow(this->X, 2) + qPow(this->Y, 2));
}

double VPoint::angle() const
{
    return qAtan2(this->Y, this->X);
}

double VPoint::x() const
{
    return this->X;
}

double VPoint::y() const
{
    return this->Y;
}

void VPoint::setLength(double length)
{
    qreal angle = qAtan2(this->Y, this->X);
    this->X = length * qCos(angle);
    this->Y = length * qSin(angle);
}

void VPoint::setAngle(double angle)
{
    qreal length = qSqrt(qPow(this->X, 2) + qPow(this->Y, 2));
    this->X = length * qCos(angle);
    this->Y = length * qSin(angle);
}

void VPoint::setX(double x)
{
    this->X = x;
}

void VPoint::setY(double y)
{
    this->Y = y;
}

VPoint & VPoint::operator += (const VPoint &point)
{
    this->X += point.X;
    this->Y += point.Y;
    return *this;
}

VPoint & VPoint::operator -= (const VPoint &point)
{
    this->X -= point.X;
    this->Y -= point.Y;
    return *this;
}

VPoint & VPoint::operator = (double value)
{
    this->setLength(value);
    return *this;
}

VPoint & VPoint::operator += (double value)
{
    this->setLength(this->length() + value);
    return *this;
}

VPoint & VPoint::operator -= (double value)
{
    this->setLength(this->length() - value);
    return *this;
}

VPoint & VPoint::operator *= (double value)
{
    this->setLength(this->length() * value);
    return *this;
}

VPoint & VPoint::operator /= (double value)
{
    this->setLength(this->length() / value);
    return *this;
}


bool operator != (const VPoint &p1, const VPoint &p2)
{
    if(p1.X == p2.X && p1.Y == p2.Y)
    {
        return false;
    }
    else
    {
        return true;
    }
}

bool operator == (const VPoint &p1, const VPoint &p2)
{
    if(p1.X == p2.X && p1.Y == p2.Y)
    {
        return true;
    }
    else
    {
        return false;
    }
}

const VPoint operator + (const VPoint &p1, const VPoint &p2)
{
    return VPoint(p1.X + p2.X, p1.Y + p2.Y);
}

const VPoint operator - (const VPoint &p1, const VPoint &p2)
{
    return VPoint(p1.X - p2.X, p1.Y - p2.Y);
}

//Qt专用部分
VPoint::VPoint(const QPointF &point):X(point.x()),Y(point.y()){}

void VPoint::fromQPointF(const QPointF &point)
{
    this->X = point.x();
    this->Y = point.y();
}

QPointF VPoint::toQPointF() const
{
    return QPointF(this->X, this->Y);
}
